package com.huikeportal.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huikeportal.controller.common.Lang;
import com.huikeportal.model.ChangeGcParam;
import com.huikeportal.model.KequnAnalysisModel;
import com.huikeportal.model.Language;
import com.huikeportal.model.ReportGraphData;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.model.SiteSimpleInfo;
import com.huikeportal.model.SquareChange;
import com.huikeportal.model.UserInfo;
import com.huikeportal.model.WordBook;
import com.huikeportal.model.common.SqlParameter;
import com.huikeportal.service.FootfallGlobalService;
import com.huikeportal.service.LoginService;
import com.huikeportal.service.SelectSiteService;
import com.huikeportal.service.SquareKequnService;
import com.huikeportal.service.TodayIndexesService;


/**
 * 
 * @author wyp
 *	客群分析
 */

@Controller
public class SquareKequnController {



	@Autowired
	private HttpSession session;

	@Autowired
	private LoginService loginService;

	@Autowired
	private FootfallGlobalService footfallGlobalService;
	
	@Autowired
	private SquareKequnService squareKequnService;
	
	@Autowired
	private TodayIndexesService todayIndexesService;

	@Autowired
	SelectSiteService selectSiteService;

	/**初始化全局分析页面  */
	@RequestMapping("/squareKequn")
	public String squareKequn( ModelMap model,String siteId,HttpServletRequest request,HttpServletResponse response) throws Exception {
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		userInfo = loginService.getUserInfo(userInfo);//得到用户信息
		String userId=userInfo.getUserId();
		 //获取当前系统语言
        String sysLang = session.getAttribute("sysLang").toString();
        //查询当前页面id
     
        SqlParameter sql = new SqlParameter();
        sql.setSysLang(sysLang);
        sql.setString("squareKequn");
        Integer menuId = selectSiteService.getMenuId(sql);
        //获取用户权限按钮信息
        List<WordBook> buttonList = loginService.getUserButtonList(sysLang,
                userId, menuId);
        String buttonExport = "0";
        for(WordBook wordBook:buttonList){
        	if("导出".equals(wordBook.getDicValue())){
        		buttonExport = "1";
        		break;
        	}
        }
       
        /** 选择场所初始化  **/
		// 更换广场div初始化数据
		List<SiteAllInfo> dpGcList = loginService.getDpGcSiteList(userInfo);
		ChangeGcParam changeGcParam = new ChangeGcParam();
		changeGcParam.setSelectType("0");// 单选场所
		List<SiteAllInfo> yuanGcList = loginService.getGcSiteList(userInfo);
		SquareChange squareChange = selectSiteService.squareChangeLoad(
				changeGcParam, dpGcList,yuanGcList);// 获取初始化更换广场信息
		model.addAttribute("squareChange", squareChange);// 更换广场数据初始化
		model.addAttribute("selectType", changeGcParam.getSelectType());// 选择类型，1为多选
		model.addAttribute("pageType", "1");// 页面类型，带集团，0表示不带集团
		model.addAttribute("backPage", "squareKequn");// 返回地址
		// 初始化查询集团&地区列表
		SqlParameter sqlParam = new SqlParameter();
		sqlParam.setString(userInfo.getUserId());
		List<SiteAllInfo> jtDqList = selectSiteService.getJtDqList(sqlParam);
		model.addAttribute("jtDqList", jtDqList);
		if (siteId != null && !"".equals(siteId)) {
			// 根据场所id查询场所信息
			List<SiteSimpleInfo> selectedSiteList = selectSiteService
					.getSimpleSiteList(siteId);
			// 将已选择的场所传回到页面上
			model.addAttribute("selectedSiteList", JSONArray.fromObject(
					selectedSiteList).toString());
		} 
        
		String siteType=FootfallConstant.WHOLE_SITETYPE;//场所类型是300
		SiteAllInfo siteAllInfo=footfallGlobalService.selectSiteByUserId(userInfo,siteType);
//		siteId = "BBBGC";
		String siteID="";//测试值
		if(siteId!=null&& !siteId.equals("")){
			siteID=siteId;
		}else{
			model.addAttribute("datetime", footfallGlobalService.getDate());
			Language lang = Lang.getInfo(sysLang);
			model.addAttribute("lang",lang);
			String menuHtml = loginService.getMenuHtml(userInfo, "squareKequn");//设置左侧列表菜单
			model.addAttribute("menuHtml", menuHtml);// 左侧菜单html
			return "squareKequn";
		}
		siteId = siteID;
		
		if(siteID!=null&& !siteID.equals("")){
			String siteName=todayIndexesService.getSiteNameBySiteId(siteID);
			String siteNames=footfallGlobalService.getSiteNames(siteID);
			model.addAttribute("siteName", siteNames);
			model.addAttribute("siteId", siteID);
			model.addAttribute("datetime", footfallGlobalService.getDate());
		}
		model.addAttribute("buttonExport",buttonExport);
		Language lang = Lang.getInfo(sysLang);
		model.addAttribute("lang",lang);
		String menuHtml = loginService.getMenuHtml(userInfo, "squareKequn");//设置左侧列表菜单
		model.addAttribute("menuHtml", menuHtml);// 左侧菜单html
		return "squareKequn";
	}
	
	/**前台获取图表数据  */
	@RequestMapping("/getSquareKequnData")
	@ResponseBody
	public HashMap<String , Object> getGraphData(HttpServletRequest request,HttpServletResponse response) throws Exception {
		
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		userInfo = loginService.getUserInfo(userInfo);//得到用户信息
		String days= request.getParameter("span").toString();//获取时间及其类型
		String[] Strings =days.split("_");
		//日周月年
		String timeType_table=Strings[0];
		//类型1、2小时，7、14、30天；4、8、12周；3、6、12月；3、5年
		String timeType=Strings[1];
		//图表的开始时间
		String startDate=request.getParameter("from").toString();//获取几天的数据
		//如表的结束时间
		String endDate=request.getParameter("to").toString();//获取几天的数据
		
		String siteId=request.getParameter("siteId");//获取几天的数据
		HashMap<String , Object> paraMapResult=new HashMap<String, Object>();
		
		//查询图表
		HashMap<String , Object> paraMap=new HashMap<String, Object>();
		paraMap.put("timeType", timeType);
		paraMap.put("timeType_table", timeType_table);//日周月年表
		paraMap.put("startDate", startDate);
		paraMap.put("endDate", endDate);
		paraMap.put("siteId", siteId);
		paraMap.put("userInfo", userInfo);
		List<ReportGraphData> list= footfallGlobalService.getGraphData(paraMap);
		
		List<String> siteType = footfallGlobalService.getSiteTypes(siteId);
		/** 获取siteId 的场所类型中文名称*/
		String siteTypeName = "";
		if(siteType!=null&&!"".equals(siteType)){
			String siteType1 = siteType.get(0);
			siteTypeName = footfallGlobalService.getSiteTypeName(Integer.parseInt(siteType1));
		}
		paraMapResult.put("siteTypeName", siteTypeName);
		paraMapResult.put("stadubiao", list.get(0).getDatatime());
		paraMapResult.put("endtubiao", list.get(list.size()-1).getDatatime());
		/*查询比较图表*/
		
		//插入数据日期类型，日周年月
		paraMapResult.put("timeType_table",timeType_table);
		//插入折线数据
		paraMapResult.put("result", list);
		//获取表格数据
		StringBuffer loadTable = getGlobalTable(list,timeType_table,userInfo,siteId);//利用插件型
		//放入表格数据
		paraMapResult.put("loadTable", loadTable);
		String siteNames=footfallGlobalService.getSiteNames(siteId);//前台导航栏地址 广场-场所
		paraMapResult.put("siteN", siteNames);
		String sitename=footfallGlobalService.getSiteName(siteId);//siteId的中文名称
		paraMapResult.put("siteName", sitename);
		
		//获取pei图新老顾客数据
		HashMap<String, Double> peiNweOld = squareKequnService.getPeiNweOld(list);
		paraMapResult.put("peiNweOld", peiNweOld);
		//获取首末到店时间数据
		List<KequnAnalysisModel> retReportGraphData = squareKequnService.getStartEndCome(paraMap);
		paraMapResult.put("retReportGraphData", retReportGraphData);
		//获取pei图终端品牌数据
		HashMap<String, Double> phoneTerminal = squareKequnService.getPeiPhoneTerminal(paraMap);
		paraMapResult.put("phoneTerminal",phoneTerminal);
		//获取pei图到店频率占比数据
		HashMap<String, Double> comeFreq = squareKequnService.getPeiComeFreq(paraMap);
		paraMapResult.put("comeFreq",comeFreq);
		//获取pei图滞留时间数据
		HashMap<String, Double> retentionavg = squareKequnService.getPeiRetentionavg(paraMap);
		paraMapResult.put("retentionavg",retentionavg);
		
		return paraMapResult;
	}
	
	/**前台获取图表数据  */
	public StringBuffer getGlobalTable(List<ReportGraphData> list,String timeType_table,UserInfo userInfo,String siteId) throws Exception {
		return squareKequnService.getGlobalTable(list,timeType_table,userInfo,siteId);
	}
}
